package com.example.SBootDemo.service.Imp.function;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import com.example.SBootDemo.Dao.Interface.funDao;
import com.example.SBootDemo.Dao.Interface.roleDao;
import com.example.SBootDemo.ExceptionHandel.exception.Myexception;
import com.example.SBootDemo.Util.TableTitles;
import com.example.SBootDemo.Util.funplace;
import com.example.SBootDemo.model.function.function;
import com.example.SBootDemo.model.function.role;
import com.example.SBootDemo.model.function.user;
import com.example.SBootDemo.service.Interface.function.roleinterface;
@Service
public class roleimpl implements roleinterface {
	@Autowired
	roleDao role;
	@Autowired
	funDao fun;

	@Override
	public int getRoles(int pageinfo, HttpServletRequest request) throws Myexception {
		// TODO Auto-generated method stub
		int flag=0;
		
		Sort sort = new Sort(Direction.DESC, "id");
	    Pageable pageable = new PageRequest(pageinfo, TableTitles.getPagesize(), sort);
	    Page page = null;
		
			try {
				page = role.findAll(pageable);
				flag=1;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				throw new Myexception("分页查询role方法出错");
			}
			 
		    //面包屑导航
	    funplace funplace = new funplace("系统管理","用户与权限","角色列表");
	    List<String> titles = TableTitles.getRoleTitles();
	    //列表信息
		 request.setAttribute("List", page.getContent());
		//列表栏目信息
		 request.setAttribute("titles", titles);
		 //分页总页数
		 request.setAttribute("pages", page.getTotalPages());
		 //当前页，spring date jpa默认从0算起
		 request.setAttribute("number", page.getNumber());
	   
		 request.setAttribute("funplace", funplace);
		return  flag;
	}

	//跳转到添加角色页面
	@Override
	public void addpage(HttpServletRequest request) {
		// TODO Auto-generated method stub
		 funplace funplace = new funplace("系统管理","用户与权限","新建角色");
		  request.setAttribute("funplace", funplace);
		  List<function> functions=fun.findAll();
			
		  //空user 前台模板取null有用
		  request.setAttribute("role", new role());
		  //前段角色下拉
		  request.setAttribute("functions", functions);
	}
	/*************************角色信息修改页面准备**************************************************************/
	@Override
	public void updatepage(HttpServletRequest request, int id) throws Myexception {
		// TODO Auto-generated method stub
		 role roles = role.findById(id);
		 funplace funplace = new funplace("系统管理","角色管理","角色信息修改");
		 List<function> functions=fun.findAll();
		 request.setAttribute("role", roles);
	     request.setAttribute("updatefunctions", functions);
	     request.setAttribute("funplace", funplace);
		
	}
	/*************************角色信息修改&&重新分配权限**************************************************************/
	@Override
	public void updateRole(role roles, int[] ckid,
			HttpServletRequest request) throws Myexception {
		List<function> funlist = new ArrayList<>();
		for(int i=0;i<ckid.length;i++){
			
			funlist.add(fun.findById(ckid[i]));
		}
		roles.setFunlist(funlist);
		try {
			role.save(roles);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new Myexception("更新角色信息方法出错");
		}
		
	}
	/*************************角色删除**************************************************************/
	@Override
	@Transactional
	public void deleteRole(List<Integer> listid) throws Myexception {
		// TODO Auto-generated method stub
		try {
			for(int i=0;i<listid.size();i++){
				role.deleteById(listid.get(i));
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new Myexception("角色删除功能出错");
		}
	}

}
